﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using AnalysisApp.Web.Models;
using UILibrary.Olap.UITestApplication.Web;

namespace AnalysisApp.Web.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        [System.Web.Mvc.HttpPost]
        public string SetOLAPConnectionStringAndCheck(string connection)
        {
            string result;
            try
            {
                OlapWebService olapWebService = new OlapWebService();
                result = olapWebService.PerformOlapServiceAction("SETCONNECTIONSTRING", connection);
            }
            catch (Exception e)
            {
                HttpContext.Response.AddHeader("error", "yes");

                return e.Message;
            }
            return result;
        }

        [System.Web.Mvc.HttpPost]
        public dynamic GetCubes(OlapMetaDataHelper.OlapData par)
        {
            dynamic result;
            try
            {
                OlapMetaDataHelper olapMetaDataHelper = new OlapMetaDataHelper();
                result = new JsonResult() { Data = olapMetaDataHelper.GetMetaData(par) };
            }
            catch (Exception e)
            {
                HttpContext.Response.AddHeader("error", "yes");

                return e.Message;
            }
            return result;
        }



        public ActionResult Configuration()
        {
            ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";

            return View(new ConfigurationModel());
        }


        [System.Web.Mvc.HttpPost]
        public ActionResult CreateConfiguration(ConfigurationModel configurationModel)
        {
            HttpContext.Session["configurationModel"] = configurationModel;
            return RedirectToAction("PivotTable", "Home");
        }

        public ActionResult MdxDesignerSetting()
        {
            ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";

            return View(new MDXDesignerSettingModel());
        }

        [System.Web.Mvc.HttpPost]
        public ActionResult CreateMdxDesignerSetting(MDXDesignerSettingModel mdxDesignerSettingModel)
        {
            HttpContext.Session["mdxDesignerSettingModel"] = mdxDesignerSettingModel;
            return RedirectToAction("PivotTable", "Home");
        }

        public ActionResult PivotTable()
        {
            ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
            ViewBag.mdxDesignerSettingModel = HttpContext.Session["mdxDesignerSettingModel"] ?? new MDXDesignerSettingModel();
            ViewBag.configurationModel = HttpContext.Session["configurationModel"] ?? new ConfigurationModel();

            ViewBag.GetConnectionFromParams = new bool();
            ViewBag.GetConnectionFromParams = true;


            return View();
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your app description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }

        public ActionResult Choice()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}
